﻿Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Namespace System.Workflow.ComponentModel.Design
    Friend MustInherit Class DefaultWorkflowLayout
        Inherits WorkflowLayout
        ' Methods
        Public Sub New(ByVal serviceProvider As IServiceProvider)
            MyBase.New(serviceProvider)
        End Sub

        Public Overrides Function IsCoOrdInLayout(ByVal logicalCoOrd As Point) As Boolean
            Return True
        End Function

        Public Overrides Function MapInCoOrdToLayout(ByVal logicalPoint As Point) As Point
            Return logicalPoint
        End Function

        Public Overrides Function MapInRectangleToLayout(ByVal logicalRectangle As Rectangle) As Rectangle
            Return logicalRectangle
        End Function

        Public Overrides Function MapOutCoOrdFromLayout(ByVal logicalPoint As Point) As Point
            Return logicalPoint
        End Function

        Public Overrides Function MapOutRectangleFromLayout(ByVal logicalRectangle As Rectangle) As Rectangle
            Return logicalRectangle
        End Function

        Public Overrides Sub OnPaint(ByVal e As PaintEventArgs, ByVal viewPortData As ViewPortData)
            Dim graphics As Graphics = e.Graphics
            Dim memoryBitmap As Bitmap = viewPortData.MemoryBitmap
            Dim rect As New Rectangle(Point.Empty, memoryBitmap.Size)
            graphics.FillRectangle(AmbientTheme.WorkspaceBackgroundBrush, rect)
            If (((Not MyBase.parentView.RootDesigner Is Nothing) AndAlso (MyBase.parentView.RootDesigner.Bounds.Width >= 0)) AndAlso (MyBase.parentView.RootDesigner.Bounds.Height >= 0)) Then
                Dim container As GraphicsContainer = graphics.BeginContainer
                Dim matrix As New Matrix
                matrix.Scale(viewPortData.Scaling.Width, viewPortData.Scaling.Height, MatrixOrder.Prepend)
                Dim pts As Point() = New Point() { viewPortData.LogicalViewPort.Location }
                matrix.TransformPoints(pts)
                matrix.Translate(CSng((-pts(0).X + viewPortData.ShadowDepth.Width)), CSng((-pts(0).Y + viewPortData.ShadowDepth.Height)), MatrixOrder.Append)
                graphics.Transform = matrix
                [Using] region As Region = New Region(ActivityDesignerPaint.GetDesignerPath(MyBase.parentView.RootDesigner, False))
                    Dim clip As Region = graphics.Clip
                    graphics.Clip = [region]
                    Dim ambientTheme As AmbientTheme = WorkflowTheme.CurrentTheme.AmbientTheme
                    graphics.FillRectangle(Brushes.White, MyBase.parentView.RootDesigner.Bounds)
                    If (Not ambientTheme.WorkflowWatermarkImage Is Nothing) Then
                        ActivityDesignerPaint.DrawImage(graphics, ambientTheme.WorkflowWatermarkImage, MyBase.parentView.RootDesigner.Bounds, New Rectangle(Point.Empty, ambientTheme.WorkflowWatermarkImage.Size), ambientTheme.WatermarkAlignment, 0.25!, False)
                    End If
                    graphics.Clip = clip
                End [Using]
                graphics.EndContainer(container)
            End If
        End Sub

        Public Overrides Sub OnPaintWorkflow(ByVal e As PaintEventArgs, ByVal viewPortData As ViewPortData)
            Dim graphics As Graphics = e.Graphics
            Dim memoryBitmap As Bitmap = viewPortData.MemoryBitmap
            Dim destination As New Rectangle(Point.Empty, memoryBitmap.Size)
            ActivityDesignerPaint.DrawImage(graphics, memoryBitmap, destination, destination, DesignerContentAlignment.Fill, 1!, WorkflowTheme.CurrentTheme.AmbientTheme.DrawGrayscale)
        End Sub

        Public Overrides Sub Update(ByVal graphics As Graphics, ByVal reason As LayoutUpdateReason)
        End Sub


        ' Properties
        Public Overrides ReadOnly Property Extent As Size
            Get
                Dim size As Size = If((Not MyBase.parentView.RootDesigner Is Nothing), MyBase.parentView.RootDesigner.Size, Size.Empty)
                Dim size2 As New Size((size.Width + (DefaultWorkflowLayout.Separator.Width * 2)), (size.Height + (DefaultWorkflowLayout.Separator.Height * 2)))
                Dim viewPortSize As Size = MyBase.parentView.ViewPortSize
                Return New Size(Math.Max(size2.Width, viewPortSize.Width), Math.Max(size2.Height, viewPortSize.Height))
            End Get
        End Property

        Public Overrides ReadOnly Property RootDesignerAlignment As Point
            Get
                Return New Point(DefaultWorkflowLayout.Separator)
            End Get
        End Property

        Public Overrides ReadOnly Property Scaling As Single
            Get
                Return 1!
            End Get
        End Property


        ' Fields
        Public Shared Separator As Size = New Size(30, 30)
    End Class
End Namespace

